package com.tinymall.controller;


import com.tinymall.common.ResponseData;
import com.tinymall.common.ResponseDataUtils;
import com.tinymall.pojo.Comment;
import com.tinymall.pojo.DetailsComment;
import com.tinymall.service.CommentService;
import com.tinymall.util.FilterText;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /***
     *根据指定商品ID获取评论信息
     * @param productId
     * @return
     *
     */
    @GetMapping("/{productId}")
    public List<DetailsComment> findCommentsById(@PathVariable("productId")
                                                         BigInteger productId) {
        return commentService.findCommentsById(productId);
    }

    /***
     * 查询某个商品所有的评论数
     * @param productId
     * @return
     */
    @GetMapping("/count/{productId}")
    public BigInteger getCountById(@PathVariable("productId")
                                           BigInteger productId) {
        return commentService.getCountById(productId);
    }

    /***
     * 添加评论
     * @param comment
     * @return
     */
    @PostMapping
    public ResponseData<?> insertComment(@RequestBody Comment comment) {
        String s = FilterText.replaceText(comment.getCommentText());
        comment.setCommentText(s);
        commentService.insertComment(comment);
        return ResponseDataUtils.buildSuccess("0", "评论成功!");
    }

    /***
     * 评论删除
     * @param commentId
     * @return
     */
    @DeleteMapping("/{commentId}")
    public ResponseData<?> deleteComment(@PathVariable BigInteger commentId) {
        commentService.deleteComment(commentId);
        return ResponseDataUtils.buildSuccess("0", "删除评论成功");
    }
}
